//
//  LMVisitorView.swift
//  WeiBo
//
//  Created by Li MAc on 2018/11/17.
//  Copyright © 2018 Li MAc. All rights reserved.
//

import UIKit

class LMVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置可选是首页不需要传值，已经设置
    var visitorDict : [String : String]?{
        didSet {
            guard let imgName = visitorDict?["imgName"],
                let message = visitorDict?["message"] else{
                    return
            }
            //设置标题
            biao.text = message
            //首页不需要传 信息从Base中调用
            if imgName == "" {
                //首页的才添加动画
                donghua()
                return
            }
            fang.image = UIImage(named: imgName)
            //下面两个在其他页面会有显示，将其隐藏
            zhuan.isHidden = true
            ying.isHidden = true
            
        }
    }
    
    
    
    private lazy var fang : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var zhuan : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var ying : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var biao : UILabel = UILabel.cz_label(withText: "登陆看你想看的，常回来看看", fontSize: 16, color: UIColor.gray)
    
    private lazy var registerBut : UIButton = UIButton.cz_textButton("注册", fontSize: 14, normalColor: UIColor.orange, highlightedColor: UIColor.orange, backgroundImageName: "common_button_white_disable")

    
    private lazy var loginBut : UIButton = UIButton.cz_textButton("登陆", fontSize: 14, normalColor: UIColor.gray, highlightedColor: UIColor.gray, backgroundImageName: "common_button_white_disable")
    
}

extension LMVisitorView {
    
    func setupUI(){
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
    //标题文本居中显示
        biao.textAlignment = .center
        addSubview(fang)
        addSubview(zhuan)
        addSubview(ying)
        addSubview(biao)
//        registerBut.addTarget(self, action: #selector(loginButItem), for: <#T##UIControl.Event#>)
        addSubview(registerBut)
        addSubview(loginBut)
        
        //自动布局需要 取消控件的autoresizing
        
        for i in subviews{
            i.translatesAutoresizingMaskIntoConstraints = false
        }
        
        /*
        NSLayoutConstraint(item: 视图,
                           attribute: 约束属性,
                           relatedBy: 约束关系,
                           toItem: 参照视图,
                           attribute: 参照属性,
                           multiplier: 乘积,
                           constant: 约束数值)
         
         VLF 可视化格式语言
         
         H : 水平方向
         V : 垂直方向
         | 边界
         [] 包含空间的名称字符串, 对应关系在views字典中定义
         () 定义空间的宽/高, 可以再metrics中指定, 具体是宽还是高, 看布局方向
         
        */
        
        
        addConstraint(NSLayoutConstraint(item: fang,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: fang,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        
        addConstraint(NSLayoutConstraint(item: zhuan,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: zhuan,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: fang,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: biao,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: biao,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: zhuan,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        //参照属性选择.notAnAttribute
        addConstraint(NSLayoutConstraint(item: biao,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        
        addConstraint(NSLayoutConstraint(item: registerBut,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: biao,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBut,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: biao,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        addConstraint(NSLayoutConstraint(item: registerBut,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        
        addConstraint(NSLayoutConstraint(item: loginBut,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: biao,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBut,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: biao,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBut,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        
        let ImgDict = [ "ying" :ying , "loginBut" : loginBut ]
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[ying]-0-|", options: [], metrics: nil, views: ImgDict))
         addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[ying]-(-35)-[loginBut]", options: [], metrics: nil, views: ImgDict))
        
        
        
        
    }
    
    
    private func donghua(){
        let anm = CABasicAnimation(keyPath:"transform.rotation" )
        anm.toValue = 2 * M_PI 
        anm.duration = 6
        anm.repeatCount = MAXFLOAT
        //w点击其他不会移除动画 动画完成不删除
        anm.isRemovedOnCompletion = false
        
        zhuan.layer.add(anm, forKey: nil)
    }
    
}

